package cn.tedu.micro.demo.redis;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;

/**
 * 序列化测试
 */
@SpringBootTest(classes = RedisDemo.class)
public class RedisTestDemo02 {

    @Autowired
    private RedisTemplate redisTemplate;
    //redisTemplate 目前默认序列化方式是java序列化
    //set name wanglaoshi
    @Test
    public void testSet(){
        //拿到底层连接
        RedisConnection connection =
                redisTemplate.getConnectionFactory().getConnection();
        //绕开上层封装 执行命令 完全等于 客户端语法命令
        connection.stringCommands()
                .set("name".getBytes(StandardCharsets.UTF_8)
                    ,"王翠花".getBytes(StandardCharsets.UTF_8));
    }
    //get name
    @Test
    public void setGet(){
        RedisConnection connection =
                redisTemplate.getConnectionFactory().getConnection();
        //get name 保证key值的序列化和当初存储是一样的才可以读到
        byte[] bytes1 = connection.stringCommands().get("name".getBytes(StandardCharsets.UTF_8));
        //数据读取返序列化
        String value=new String(bytes1, StandardCharsets.UTF_8);
        System.out.println(value);

        byte[] bytes2 = connection.stringCommands().get(RedisSerializer.string().serialize("name"));
        value=RedisSerializer.string().deserialize(bytes2);
        System.out.println(value);
    }
    //SET age 18
    //get age
    @Test
    public void setAndGet(){
        RedisConnection connection =
                redisTemplate.getConnectionFactory().getConnection();
        //set age 18
        connection.stringCommands().set
                (RedisSerializer.string().serialize("age"),
                 RedisSerializer.string().serialize("18"));
        //exists age
        Boolean exists = connection.keyCommands().exists(RedisSerializer.string().serialize("age"));
        System.out.println(exists);
    }
    //所以数据类型Object可以使用现成序列化java
    //数据类型使用java序列化必须实现Serializable接口
    @Test
    public void objectSer(){
        String key="product_1111";
        ProductCachePO value=new ProductCachePO("手机",new BigDecimal(33.33));
        RedisConnection connection =
                redisTemplate.getConnectionFactory().getConnection();
        connection.stringCommands().set(
                RedisSerializer.string().serialize(key),
                RedisSerializer.java().serialize(value)
        );
        byte[] bytes = connection.stringCommands().get(RedisSerializer.string().serialize(key));
        ProductCachePO cachePO = (ProductCachePO)RedisSerializer.java().deserialize(bytes);
        System.out.println(cachePO);
    }
}
